package com.business.log;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.beans.log.LogInfo;
import com.sql.DBFactory;

public class LogListBus {

	private Connection conn = null;

	private Statement stmt = null;

	private ResultSet rs = null;

	private static final Log log = LogFactory.getLog(LogListBus.class);

	// ��̨ʹ��
	@SuppressWarnings("unchecked")
	public List getAllLogByUserName(String username) {
		List list = new ArrayList();
		String sql = "select * from log_info where   log_author='" + username
				+ "' order by log_no desc";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				LogInfo li = new LogInfo();
				li.setIsQuote(rs.getString("isQuote"));// �Ƿ��Ƽ�
				li.setIsShow(rs.getString("isShow"));
				li.setLogAuthor(rs.getString("log_author"));
				li.setLogCategory(rs.getString("log_category"));
				li.setLogContent(rs.getString("log_content"));
				li.setLogId(rs.getString("log_id"));
				li.setLogIntro(rs.getString("log_intro"));
				li.setLogNo(rs.getString("log_no"));
				li.setLogPostTime(rs.getString("log_postTime"));
				li.setLogQuoteId(rs.getString("log_quote_id"));// �Ƽ�
				li.setLogQuotenums(rs.getString("log_quotenums"));// �ۺ��7�
				li.setLogTitle(rs.getString("log_title"));
				li.setLogtypeId(rs.getString("logtype_id"));
				li.setViewnums(rs.getString("viewnums"));
				li.setReplynum(rs.getString("replynum"));
				li.setLogTag(rs.getString("log_tag"));
				list.add(li);
			}
		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return list;
	}

      @SuppressWarnings("unchecked")
	public List getAllLogByShow(String username) {
		List list = new ArrayList();
		String sql = "select * from log_info where isShow=1  and log_author='"
				+ username + "' order by log_no desc";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				LogInfo li = new LogInfo();
				li.setIsQuote(rs.getString("isQuote"));// �Ƿ��Ƽ�
				li.setIsShow(rs.getString("isShow"));
				li.setLogAuthor(rs.getString("log_author"));
				li.setLogCategory(rs.getString("log_category"));
				li.setLogContent(rs.getString("log_content"));
				li.setLogId(rs.getString("log_id"));
				li.setLogIntro(rs.getString("log_intro"));
				li.setLogNo(rs.getString("log_no"));
				li.setLogPostTime(rs.getString("log_postTime"));
				li.setLogQuoteId(rs.getString("log_quote_id"));// �Ƽ�
				li.setLogQuotenums(rs.getString("log_quotenums"));// �ۺ��7�
				li.setLogTitle(rs.getString("log_title"));
				li.setLogtypeId(rs.getString("logtype_id"));
				li.setViewnums(rs.getString("viewnums"));
				li.setReplynum(rs.getString("replynum"));
				li.setLogTag(rs.getString("log_tag"));
				list.add(li);
			}
		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public List getAllLogByTypeID(int logtypeid) {
		List list = new ArrayList();
		String sql = "select * from log_info where isShow=1 and logtype_id="
				+ logtypeid + " order by log_postTime desc";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				LogInfo li = new LogInfo();
				li.setIsQuote(rs.getString("isQuote"));// �Ƿ��Ƽ�
				li.setIsShow(rs.getString("isShow"));
				li.setLogAuthor(rs.getString("log_author"));
				li.setLogCategory(rs.getString("log_category"));
				li.setLogContent(rs.getString("log_content"));
				li.setLogId(rs.getString("log_id"));
				li.setLogIntro(rs.getString("log_intro"));
				li.setLogNo(rs.getString("log_no"));
				li.setLogPostTime(rs.getString("log_postTime"));
				li.setLogQuoteId(rs.getString("log_quote_id"));// �Ƽ�
				li.setLogQuotenums(rs.getString("log_quotenums"));// �ۺ��7�
				li.setLogTitle(rs.getString("log_title"));
				li.setLogtypeId(rs.getString("logtype_id"));
				li.setViewnums(rs.getString("viewnums"));
				li.setReplynum(rs.getString("replynum"));
				li.setLogTag(rs.getString("log_tag"));
				list.add(li);

			}
		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return list;
	}

	public LogInfo getLogByNo(String logNo) {
		LogInfo li = new LogInfo();
		String sql = "select * from log_info where  log_no='" + logNo + "'";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {

				li.setIsQuote(rs.getString("isQuote"));// �Ƿ��Ƽ�
				li.setIsShow(rs.getString("isShow"));
				li.setLogAuthor(rs.getString("log_author"));
				li.setLogCategory(rs.getString("log_category"));
				li.setLogContent(rs.getString("log_content"));
				li.setLogId(rs.getString("log_id"));
				li.setLogIntro(rs.getString("log_intro"));
				li.setLogNo(rs.getString("log_no"));
				li.setLogPostTime(rs.getString("log_postTime"));
				li.setLogQuoteId(rs.getString("log_quote_id"));// �Ƽ�
				li.setLogQuotenums(rs.getString("log_quotenums"));// �ۺ��7�
				li.setLogTitle(rs.getString("log_title"));
				li.setLogtypeId(rs.getString("logtype_id"));
				li.setViewnums(rs.getString("viewnums"));
				li.setReplynum(rs.getString("replynum"));
				li.setLogTag(rs.getString("log_tag"));

			}
		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return li;
	}

	@SuppressWarnings("unchecked")
	public List getLogByTag(String logTag) {
		List list = new ArrayList();
		StringTokenizer st = new StringTokenizer(logTag, ",");
		String str = logTag;
		String sql = null;
		String tagStr = "";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			for (int i = 0; i < st.countTokens(); i++) {
				str = st.nextToken();
				sql = "select * from log_info where isShow=1 and log_tag like'%"
						+ str.trim() + "%'";
				rs = stmt.executeQuery(sql);
				while (rs.next()) {
					LogInfo li = new LogInfo();
					li.setIsQuote(rs.getString("isQuote"));// �Ƿ��Ƽ�
					li.setIsShow(rs.getString("isShow"));
					li.setLogAuthor(rs.getString("log_author"));
					li.setLogCategory(rs.getString("log_category"));
					li.setLogContent(rs.getString("log_content"));
					li.setLogId(rs.getString("log_id"));
					li.setLogIntro(rs.getString("log_intro"));
					li.setLogNo(rs.getString("log_no"));
					li.setLogPostTime(rs.getString("log_postTime"));
					li.setLogQuoteId(rs.getString("log_quote_id"));// �Ƽ�
					li.setLogQuotenums(rs.getString("log_quotenums"));// �ۺ��7�
					li.setLogTitle(rs.getString("log_title"));
					li.setLogtypeId(rs.getString("logtype_id"));
					li.setViewnums(rs.getString("viewnums"));
					li.setReplynum(rs.getString("replynum"));
					li.setLogTag(rs.getString("log_tag"));

					tagStr += li.getLogNo() + ",";

					if (tagStr.indexOf(li.getLogNo()) == tagStr.lastIndexOf(li
							.getLogNo()))
						list.add(li);
				}
				if (list != null & list.size() > 0)
					stmt.executeUpdate("update log_tag set view_count="
							+ list.size() + " where tag_name='" + str + "'");

			}

		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return list;
	}

	public LogInfo getFrontLog(String username, String logNo) {
		LogInfo li = new LogInfo();
		String sql = "select * from log_info where log_author='" + username
				+ "' and isShow=1 and log_no <'" + logNo + "' order by log_no ";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {

				li.setIsQuote(rs.getString("isQuote"));// �Ƿ��Ƽ�
				li.setIsShow(rs.getString("isShow"));
				li.setLogAuthor(rs.getString("log_author"));
				li.setLogCategory(rs.getString("log_category"));
				li.setLogContent(rs.getString("log_content"));
				li.setLogId(rs.getString("log_id"));
				li.setLogIntro(rs.getString("log_intro"));
				li.setLogNo(rs.getString("log_no"));
				li.setLogPostTime(rs.getString("log_postTime"));
				li.setLogQuoteId(rs.getString("log_quote_id"));// �Ƽ�
				li.setLogQuotenums(rs.getString("log_quotenums"));// �ۺ��7�
				li.setLogTitle(rs.getString("log_title"));
				li.setLogtypeId(rs.getString("logtype_id"));
				li.setViewnums(rs.getString("viewnums"));
				li.setReplynum(rs.getString("replynum"));
				li.setLogTag(rs.getString("log_tag"));

			}
		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return li;
	}

	public LogInfo getNextLog(String username, String logNo) {
		LogInfo li = new LogInfo();
		String sql = "select * from log_info where  log_author='" + username
				+ "' and isShow=1 and log_no>'" + logNo + "' order by log_no";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next()) {

				li.setIsQuote(rs.getString("isQuote"));// �Ƿ��Ƽ�
				li.setIsShow(rs.getString("isShow"));
				li.setLogAuthor(rs.getString("log_author"));
				li.setLogCategory(rs.getString("log_category"));
				li.setLogContent(rs.getString("log_content"));
				li.setLogId(rs.getString("log_id"));
				li.setLogIntro(rs.getString("log_intro"));
				li.setLogNo(rs.getString("log_no"));
				li.setLogPostTime(rs.getString("log_postTime"));
				li.setLogQuoteId(rs.getString("log_quote_id"));// �Ƽ�
				li.setLogQuotenums(rs.getString("log_quotenums"));// �ۺ��7�
				li.setLogTitle(rs.getString("log_title"));
				li.setLogtypeId(rs.getString("logtype_id"));
				li.setViewnums(rs.getString("viewnums"));
				li.setReplynum(rs.getString("replynum"));
				li.setLogTag(rs.getString("log_tag"));

			}
		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return li;
	}

	public void addViewCount(String logNo) {
		LogInfo li = getLogByNo(logNo);
		int viewnums = Integer.parseInt(li.getViewnums());
		viewnums++;
		// System.out.println(viewnums);
		String sql = "update log_info set viewnums=" + viewnums
				+ " where log_no='" + logNo + "'";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the udpate loginfo exception:" + e.getMessage());
			log.error("the update loginfo sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	public int getCountByType(String username, int typeId) {
		String sql = "select * from log_info where   log_author='" + username
				+ "'";
		if (typeId == 0) {
			sql += " and log_category='0' ";
		}
		if (typeId == 1) {
			sql += " and log_category='1' ";
		}
		if (typeId == 2) {
			sql += " and log_category='2' ";
		}
		int num = 0;
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {

				num++;
			}
		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return num;
	}

	public int getCountByNum(String username, int numType) {
		String sql = null;
		if (numType == 0) {
			sql = "select replynum from log_info where   log_author='"
					+ username + "'";
		}
		if (numType == 1) {
			sql = "select viewnums from log_info where  log_author='"
					+ username + "'";
		}
		int num = 0;
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				num += rs.getInt(1);
			}
		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return num;
	}
	
	@SuppressWarnings("unchecked")
	public List getAllQuoteLog() {
		List list=new ArrayList();
		String sql = "select * from log_info where isQuote=1 and isShow=1 and log_quote_id=log_no  order by log_no desc limit 0,10 ";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				LogInfo li=new LogInfo();
				li.setIsQuote(rs.getString("isQuote"));// �Ƿ��Ƽ�
				li.setIsShow(rs.getString("isShow"));
				li.setLogAuthor(rs.getString("log_author"));
				li.setLogCategory(rs.getString("log_category"));
				li.setLogContent(rs.getString("log_content"));
				li.setLogId(rs.getString("log_id"));
				li.setLogIntro(rs.getString("log_intro"));
				li.setLogNo(rs.getString("log_no"));
				li.setLogPostTime(rs.getString("log_postTime"));
				li.setLogQuoteId(rs.getString("log_quote_id"));// �Ƽ�
				li.setLogQuotenums(rs.getString("log_quotenums"));// �ۺ��7�
				li.setLogTitle(rs.getString("log_title"));
				li.setLogtypeId(rs.getString("logtype_id"));
				li.setViewnums(rs.getString("viewnums"));
				li.setReplynum(rs.getString("replynum"));
				li.setLogTag(rs.getString("log_tag"));
				list.add(li);
			}
		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return list;
	}
	public LogInfo getQuoteLog() {
		LogInfo li=new LogInfo();
				String sql = "select * from log_info where isQuote=1 and isShow=1 and log_quote_id=log_no  order by log_no desc limit 0,1 ";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				
				li.setIsQuote(rs.getString("isQuote"));// �Ƿ��Ƽ�
				li.setIsShow(rs.getString("isShow"));
				li.setLogAuthor(rs.getString("log_author"));
				li.setLogCategory(rs.getString("log_category"));
				li.setLogContent(rs.getString("log_content"));
				li.setLogId(rs.getString("log_id"));
				li.setLogIntro(rs.getString("log_intro"));
				li.setLogNo(rs.getString("log_no"));
				li.setLogPostTime(rs.getString("log_postTime"));
				li.setLogQuoteId(rs.getString("log_quote_id"));// �Ƽ�
				li.setLogQuotenums(rs.getString("log_quotenums"));// �ۺ��7�
				li.setLogTitle(rs.getString("log_title"));
				li.setLogtypeId(rs.getString("logtype_id"));
				li.setViewnums(rs.getString("viewnums"));
				li.setReplynum(rs.getString("replynum"));
				li.setLogTag(rs.getString("log_tag"));
				
			}
		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return li;
	}
	@SuppressWarnings("unchecked")
	public List getAllNewLog() {
		List list=new ArrayList();
		String sql = "select * from log_info where  isShow=1  order by log_no desc limit 0,10 ";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				LogInfo li=new LogInfo();
				li.setIsQuote(rs.getString("isQuote"));// �Ƿ��Ƽ�
				li.setIsShow(rs.getString("isShow"));
				li.setLogAuthor(rs.getString("log_author"));
				li.setLogCategory(rs.getString("log_category"));
				li.setLogContent(rs.getString("log_content"));
				li.setLogId(rs.getString("log_id"));
				li.setLogIntro(rs.getString("log_intro"));
				li.setLogNo(rs.getString("log_no"));
				li.setLogPostTime(rs.getString("log_postTime"));
				li.setLogQuoteId(rs.getString("log_quote_id"));// �Ƽ�
				li.setLogQuotenums(rs.getString("log_quotenums"));// �ۺ��7�
				li.setLogTitle(rs.getString("log_title"));
				li.setLogtypeId(rs.getString("logtype_id"));
				li.setViewnums(rs.getString("viewnums"));
				li.setReplynum(rs.getString("replynum"));
				li.setLogTag(rs.getString("log_tag"));
				list.add(li);
			}
		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return list;
	}
	public LogInfo getNewLog() {
		LogInfo li=new LogInfo();
				String sql = "select * from log_info where isShow=1   order by log_no desc limit 0,1 ";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				
				li.setIsQuote(rs.getString("isQuote"));// �Ƿ��Ƽ�
				li.setIsShow(rs.getString("isShow"));
				li.setLogAuthor(rs.getString("log_author"));
				li.setLogCategory(rs.getString("log_category"));
				li.setLogContent(rs.getString("log_content"));
				li.setLogId(rs.getString("log_id"));
				li.setLogIntro(rs.getString("log_intro"));
				li.setLogNo(rs.getString("log_no"));
				li.setLogPostTime(rs.getString("log_postTime"));
				li.setLogQuoteId(rs.getString("log_quote_id"));// �Ƽ�
				li.setLogQuotenums(rs.getString("log_quotenums"));// �ۺ��7�
				li.setLogTitle(rs.getString("log_title"));
				li.setLogtypeId(rs.getString("logtype_id"));
				li.setViewnums(rs.getString("viewnums"));
				li.setReplynum(rs.getString("replynum"));
				li.setLogTag(rs.getString("log_tag"));
				
			}
		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return li;
	}
}
